<template>
	<view class="content">
		<!-- <view class="contract" :style="{height:+ height+'px'} "> -->
		<!-- <web-view :src="pdfUrl"></web-view> -->
		<!-- <iframe class="contract" :style="{height:height+'px'} " :src="pdfUrl"></iframe> -->

		<!-- </view> -->
		<view class="" v-if="pdfUrl">
			<mumu-previewOffce :fileUrl='pdfUrl' v-model='pdfUrl'></mumu-previewOffce>
		</view>

<!-- v-show="item.template_id!=6" -->
		<uni-link  :href="href" :text="href" showUnderLine="false" color="red">
			<view class="btn" v-if=" item.is_signature != 1 && href ">
				签字确认
			</view>
		</uni-link>

		<!-- <view class="btn" v-if=" item.is_signature != 1 && item.template_id==6" @click="Signing">
			签字确认
		</view> -->
		<confirmview ref="confirmedView" :href="href" />
	</view>
</template>

<script>
	import confirmview from "../../components/confirm/confirm"
	// import webview from "./webview"
	import {
		createbytemplatecomponents,
		handWriting,
		getSignImg,
		sign_contract,
		get_sign_list,
		download_contract
	} from "@/api/index.js"
	export default {
		components: {
			confirmview,
			// webview
		},
		data() {
			return {
				height: '160',
				item: "",
				pdfUrl: false,
				signId: "",
				sign_ids: [],
			};
		},

		onLoad(e) {
			this.item = JSON.parse(uni.getStorageSync('generalItem'))

			uni.setNavigationBarTitle({
				title: this.item.name
			});

			uni.getSystemInfo({
				success: res => {
					this.height = this.item.is_signature == 1 ? res.safeArea.height - 60 * (res.windowWidth /
						750) : res.safeArea.height - 40 * (res.windowWidth / 750) - 85
				}
			})
			if (e.signId) {
				// let signId = uni.getStorageSync("signId" + this.item.id)
				// if (signId) {
				// 	signId = JSON.parse(signId)
				// }
				// signId = [...signId, JSON.parse(e.signId)]
				this.toSign([JSON.parse(e.signId)])
				// uni.setStorageSync("signId" + this.item.id, JSON.stringify(signId))
				return
			}


			this.init()


		},
		onBackPress() {
			// console.log(111,
			// 	"onBackPressonBackPressonBackPressonBackPressonBackPressonBackPressonBackPressonBackPressonBackPressonBackPressonBackPress"
			// )
			// uni.removeStorageSync("signId")
		},
		methods: {
			// 判断签名次数
			toSign(signId) {
				this.signContract(signId)
				// get_sign_list({
				// 	sign_type: this.item.template_id
				// }).then(res => {
				// if (this.item.template_id == 1 ||
				// 	this.item.template_id == 2 ||
				// 	this.item.template_id == 3 ||
				// 	this.item.template_id == 5||
				// 	this.item.template_id == 11) {
				// 	if (signId.length == 1) {
				// 		this.item.is_signature = 1

				// 	}
				// } else if (this.item.template_id == 6 ||
				// 	this.item.template_id == 7 ||
				// 	this.item.template_id == 8) {
				// 	if (signId.length == 2) {
				// 		this.item.is_signature = 1
				// 	}
				// }  else {
				// 	if (signId.length == 3) {
				// 		this.item.is_signature = 1
				// 	}
				// }
				// })
			},

			signContract(signId) {
				sign_contract({
					id: this.item.id,
					sign_ids: JSON.stringify(signId)
				}).then(signRes => {
					// this.signStart = true;
					this.item.is_signature = 1
					uni.setStorageSync("generalItem", JSON.stringify(this.item))
					download_contract({
						id: this.item.id
					}).then(res => {
						this.pdfUrl = res.data.contract_url
					})
				}).catch(err => {
					
				})
			},
			init() {

				handWriting({
					id: this.item.id
				}).
				then(res => {
					this.href = res.data.hand_writing_url
				}).catch(err => {

				})
				
				if (this.item.is_signature == 1) {
					download_contract({
						id: this.item.id
					}).then(res => {
						this.pdfUrl = res.data.contract_url
					})
					return
				}
				createbytemplatecomponents({
					id: this.item.id
				}).then(res => {
					this.pdfUrl = res.data.contract_url
				})
			},
			Signing() {

				this.$refs.confirmedView.open()

			}
		}
	}
</script>

<style lang="less">
	@import url("../../index.less");

	.content {
		width: 690rpx;
		height: auto;
		overflow: hidden;
		background: #FFFFFF;
		border-radius: 20rpx;
		opacity: 1;
		margin: 30rpx 30rpx 36rpx;
		/* min-height: 30vh; */
		font-size: @font14;
		color: #666666;
		padding-bottom: 70rpx;

		&::-webkit-scrollbar {
			width: 0px; //修改滚动条宽度
		}

		&::-webkit-scrollbar-track {
			border-radius: 0px;

		}
	}

	.btn {
		width: 400rpx;
		height: 40px;
		background: #FF7100;
		border-radius: 20px;
		opacity: 1;
		font-size: @font16;
		color: #FFFFFF;
		line-height: 40px;
		text-align: center;
		bottom: 30rpx;
		margin: 15px 0 15px;
		left: 175rpx;
		/* margin-bottom: calc(constant(safe-area-inset-bottom)+30rpx);
		margin-bottom: calc(env(safe-area-inset-bottom)+30rpx); */

		position: fixed;
		z-index: 100;
	}
</style>